<?php

class Model_Transaction extends \Orm\Model
{

    protected static $_properties = array(
        'id',
        'category_id',
        'wallet_id',
        'parent_id',
        'money',
        'created',
        'updated',
        'deleted',
        'with_who', //'created_at', để như thế này lúc dùng creat_auth ko bị lỗi.
    );
    protected static $_observers = array(
        'Orm\Observer_CreatedAt' => array(
            'events' => array('before_insert'),
            'mysql_timestamp' => false,
        ),
        'Orm\Observer_UpdatedAt' => array(
            'events' => array('before_update'),
            'mysql_timestamp' => false,
        ),
    );
    protected static $_table_name = 'transactions';
    protected static $_belongs_to = array(
        'categories' => array(
            'key_from' => 'category_id',
            'model_to' => 'Model_Category',
            'key_to' => 'id',
            'cascade_save' => true,
            'cascade_delete' => false,
        ),
        'wallets' => array(
            'key_from' => 'wallet_id',
            'model_to' => 'Model_Wallet',
            'key_to' => 'id',
            'cascade_save' => true,
            'cascade_delete' => false,
        ),
        'parent_transaction' => array(
            'key_from' => 'parent_id',
            'model_to' => 'Model_Transaction',
            'key_to' => 'id',
            'cascade_save' => true,
            'cascade_delete' => false,
        ),
        'child_transaction' => array(
            'key_from' => 'id',
            'model_to' => 'Model_Transaction',
            'key_to' => 'parent_id',
            'cascade_save' => true,
            'cascade_delete' => false,
        ),
       
        
        
        
    );

    public static function get_validation_add(Validation $val)
    {
        $val->add_callable(new MyRules()); //thêm rules unique của mình
        $val->add_field('type', 'type','required');
        $val->add_field('category_id', 'category_id','required');
        $val->add_field('wallet_id', 'wallet id','required');
        $val->add_field('money', 'money','required|trim|max_length[20]|valid_string[numeric]');
        $val->add_field('with_who', 'With who','trim|min_length[3]|max_length[200]');
        return $val;
    }

    public static function get_validation_edit(Validation $val) // cai nay luat unique chi xet tren nhung truong khac no
    {
        $val->add_callable(new MyRules()); //thêm rules unique của mình
        $val->add_field('name', 'Wallet name', 'required|trim|min_length[3]|max_length[20]|valid_string[alpha,lowercase,numeric]');
        $val->add_field('current_money', 'Money', 'required|valid_string[numeric]');
        return $val;
    }

}
